################################################################################################
# This script takes the QV/SV results & generates 16 scatterplots (Fig. 3 in Casella et al. (2010) 
# CU elections paper; 8 for each treatment) for the number of points assigned to the issue in 
# which the BV/QV was cast vs. the max number of points assigned to the other referenda. 
# The specific criteria is as follows: 
# 
# - SV: The first calculation is the margin of victory for the number of votes (no BV or QV). The 
#   formula used is: (# votes in Majoriy - # votes in Minority)/Total Votes.  
# - QV: The second calculation is the margin of victory of aggregate points (preferences).  The 
#   formula used is: (Sum points in Majoriy - Sum points in Minority)/Total Points allocaed.  
# 
# Exports the the scatterplots to the /California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots folder. 
# 
# Author: Luis S.
# Last Modified: 7.24.16
################################################################################################

library(dplyr)
library(ggplot2)
library(gridExtra)

# setwd("C:/Users/Luis/Dropbox/Research/SV vs QV California Experiment/Experiment Analysis & Results/")

####################################
########## SV Scatterplot ##########
####################################

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean&Recoded_AllPos.csv", header = T, stringsAsFactors = F)

# holds the max points for subjects who casted the BV in the one of the "Not#####" referenda
SV_data$MaxPoints_SV1NotEducation <- NA
SV_data$MaxPoints_SV1NotTeachers <- NA
SV_data$MaxPoints_SV1NotBonds <- NA
SV_data$MaxPoints_SV1NotImmigration <- NA

SV_data$MaxPoints_SV2NotEducation <- NA
SV_data$MaxPoints_SV2NotTeachers <- NA
SV_data$MaxPoints_SV2NotBonds <- NA
SV_data$MaxPoints_SV2NotImmigration <- NA

for(row in 1:nrow(SV_data))
{
  ### Creates MaxPref Column with the highest value assigned to the complement set of proposals for each proposal
  
  if(SV_data$SV1_Vote[row] == "BilingualEduc") {SV_data$MaxPoints_SV1NotEducation[row] <- max(SV_data$TeachersPref[row], SV_data$BondsPref[row], SV_data$ImmigrationPref[row])
  } else if(SV_data$SV1_Vote[row] == "TeacherTenure") {SV_data$MaxPoints_SV1NotTeachers[row] <- max(SV_data$EducationPref[row], SV_data$BondsPref[row], SV_data$ImmigrationPref[row])
  } else if(SV_data$SV1_Vote[row]  == "PublicBonds") {SV_data$MaxPoints_SV1NotBonds[row] <- max(SV_data$EducationPref[row], SV_data$TeachersPref[row], SV_data$ImmigrationPref[row])
  } else {SV_data$MaxPoints_SV1NotImmigration[row] <- max(SV_data$EducationPref[row], SV_data$TeachersPref[row], SV_data$BondsPref[row])} 
  
  if(SV_data$SV2_Vote[row] == "BilingualEduc") {SV_data$MaxPoints_SV2NotEducation[row] <- max(SV_data$TeachersPref[row], SV_data$BondsPref[row], SV_data$ImmigrationPref[row])
  } else if(SV_data$SV2_Vote[row] == "TeacherTenure") {SV_data$MaxPoints_SV2NotTeachers[row] <- max(SV_data$EducationPref[row], SV_data$BondsPref[row], SV_data$ImmigrationPref[row])
  } else if(SV_data$SV2_Vote[row]  == "PublicBonds") {SV_data$MaxPoints_SV2NotBonds[row] <- max(SV_data$EducationPref[row], SV_data$TeachersPref[row], SV_data$ImmigrationPref[row])
  } else {SV_data$MaxPoints_SV2NotImmigration[row] <- max(SV_data$EducationPref[row], SV_data$TeachersPref[row], SV_data$BondsPref[row])} 
  
}



######################### SV1 Dot Plots ####################

#Education plot
tempSummary <- select(SV_data, EducationPref, MaxPoints_SV1NotEducation) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV1 - Education (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV1NotEducation, y = EducationPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda" ) + ylab("Points Assigned to Education") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV1 - Education.png", height = 4, width = 4.5)

#Teacher Tenure plot
tempSummary <- select(SV_data, TeachersPref, MaxPoints_SV1NotTeachers) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV1 - Teachers Tenure (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV1NotTeachers, y = TeachersPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Teachers Tenure") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV1 - Teachers Tenure.png", height = 4, width = 4.5)

#Immigration plot
tempSummary <- select(SV_data, ImmigrationPref, MaxPoints_SV1NotImmigration) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV1 - Immigration (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV1NotImmigration, y = ImmigrationPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Immigration") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV1 - Immigration.png", height = 4, width = 4.5)


# immigration 2
tempSummary <- SV_data %>% select(ImmigrationPref, MaxPoints_SV1NotImmigration, Ref4_Immigration) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV1 - Immigration (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV1NotImmigration, y = ImmigrationPref, colour = Ref4_Immigration)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Immigration") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV1 - Immigration - V2.png", height = 4, width = 6.5)

#Public Bonds plot
tempSummary <- select(SV_data, BondsPref, MaxPoints_SV1NotBonds) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV1 - Public Bonds (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV1NotBonds, y = BondsPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Public Bonds") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV1 - Public Bonds.png", height = 4, width = 4.5)


######################### SV2 Dot Plots ####################

#Education plot
tempSummary <- select(SV_data, EducationPref, MaxPoints_SV2NotEducation) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV2 - Education (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV2NotEducation, y = EducationPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Education") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV2 - Education.png", height = 4, width = 4.5)

#Teacher Tenure plot
tempSummary <- select(SV_data, TeachersPref, MaxPoints_SV2NotTeachers) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV2 - Teachers Tenure (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV2NotTeachers, y = TeachersPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Teachers Tenure") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV2 - Teachers Tenure.png", height = 4, width = 4.5)

#Immigration plot
tempSummary <- select(SV_data, ImmigrationPref, MaxPoints_SV2NotImmigration) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV2 - Immigration (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV2NotImmigration, y = ImmigrationPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Immigration") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV2 - Immigration.png", height = 4, width = 4.5)

#Public Bonds plot
tempSummary <- select(SV_data, BondsPref, MaxPoints_SV2NotBonds) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("SV2 - Public Bonds (N = ", totalSubjects, ")", sep = "")

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_SV2NotBonds, y = BondsPref)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Public Bonds") + ggtitle(plotTitle) +
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14))
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/SV2 - Public Bonds.png", height = 4, width = 4.5)




####################################
########## QV Scatterplot ##########
####################################

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean&Recoded_AllPos.csv", header = T, stringsAsFactors = F)

# holds the max points for subjects who casted the BV in the one of the "Not#####" referenda
QV_data$MaxPoints_QV1NotEducation <- NA
QV_data$MaxPoints_QV1NotTeachers <- NA
QV_data$MaxPoints_QV1NotBonds <- NA
QV_data$MaxPoints_QV1NotImmigration <- NA

QV_data$MaxPoints_QV2NotEducation <- NA
QV_data$MaxPoints_QV2NotTeachers <- NA
QV_data$MaxPoints_QV2NotBonds <- NA
QV_data$MaxPoints_QV2NotImmigration <- NA

for(row in 1:nrow(QV_data))
{
  
  ### Creates MaxPref Column with the highest value assigned to the complement set of proposals for each proposal
  
  if(!is.na(QV_data$QV1_Vote_BilingualEduc[row])) {QV_data$MaxPoints_QV1NotEducation[row] <- max(QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])} 
  if(!is.na(QV_data$QV1_Vote_TeacherTenure[row])) {QV_data$MaxPoints_QV1NotTeachers[row] <- max(QV_data$EducationPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])} 
  if(!is.na(QV_data$QV1_Vote_PublicBonds[row])) {QV_data$MaxPoints_QV1NotBonds[row] <- max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$ImmigrationPref[row])} 
  if(!is.na(QV_data$QV1_Vote_Immigration[row])) {QV_data$MaxPoints_QV1NotImmigration[row] <- max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row])} 
  
  if(!is.na(QV_data$QV2_Vote_BilingualEduc[row])) {QV_data$MaxPoints_QV2NotEducation[row] <- max(QV_data$TeachersPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])} 
  if(!is.na(QV_data$QV2_Vote_TeacherTenure[row])) {QV_data$MaxPoints_QV2NotTeachers[row] <- max(QV_data$EducationPref[row], QV_data$BondsPref[row], QV_data$ImmigrationPref[row])} 
  if(!is.na(QV_data$QV2_Vote_PublicBonds[row])) {QV_data$MaxPoints_QV2NotBonds[row] <- max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$ImmigrationPref[row])} 
  if(!is.na(QV_data$QV2_Vote_Immigration[row])) {QV_data$MaxPoints_QV2NotImmigration[row] <- max(QV_data$EducationPref[row], QV_data$TeachersPref[row], QV_data$BondsPref[row])} 
}



######################### QV1 Dot Plots ####################

#Education plot
tempSummary <- select(QV_data, EducationPref, MaxPoints_QV1NotEducation, QV1_Class) %>% rename(Class = QV1_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV1 - Education (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV1NotEducation, y = EducationPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda" ) + ylab("Points Assigned to Education") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV1 - Education.png", height = 4, width = 6)

#Teacher Tenure plot
tempSummary <- select(QV_data, TeachersPref, MaxPoints_QV1NotTeachers, QV1_Class) %>% rename(Class = QV1_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV1 - Teachers Tenure (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV1NotTeachers, y = TeachersPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Teachers Tenure") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV1 - Teachers Tenure.png", height = 4, width = 6)

#Immigration plot
tempSummary <- select(QV_data, ImmigrationPref, MaxPoints_QV1NotImmigration, QV1_Class) %>% rename(Class = QV1_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV1 - Immigration (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV1NotImmigration, y = ImmigrationPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Immigration") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV1 - Immigration.png", height = 4, width = 6)

# #Immigration plot - V2
# tempSummary <- select(QV_data, ImmigrationPref, MaxPoints_QV1NotImmigration, QV1_Class, Ref4_Immigration) %>% rename(Class = QV1_Class) %>% na.omit(tempSummary) %>% filter(Ref4_Immigration != "Abstain", Class == c("(1) Red","(2) Yellow"))
# totalSubjects <- nrow(tempSummary)
# plotTitle <- paste("QV1 - Immigration (N = ", totalSubjects, ")", sep = "")
# classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))
# 
# DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV1NotImmigration, y = ImmigrationPref, colour = Class, shape = Ref4_Immigration)) + 
#   geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
#   geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
#   xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Immigration") + ggtitle(plotTitle) +
#   scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
#   theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
#   annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
# print(DotPlot)
# ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV1 - Immigration - V2.png", height = 4, width = 6)


#Public Bonds plot
tempSummary <- select(QV_data, BondsPref, MaxPoints_QV1NotBonds, QV1_Class) %>% rename(Class = QV1_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV1 - Public Bonds (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV1NotBonds, y = BondsPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Public Bonds") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV1 - Public Bonds.png", height = 4, width = 6)


######################### QV2 Dot Plots ####################

#Education plot
tempSummary <- select(QV_data, EducationPref, MaxPoints_QV2NotEducation, QV2_Class) %>% rename(Class = QV2_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV2 - Education (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV2NotEducation, y = EducationPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Education") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV2 - Education.png", height = 4, width = 6)

#Teacher Tenure plot
tempSummary <- select(QV_data, TeachersPref, MaxPoints_QV2NotTeachers, QV2_Class) %>% rename(Class = QV2_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV2 - Teachers Tenure (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV2NotTeachers, y = TeachersPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Teachers Tenure") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV2 - Teachers Tenure.png", height = 4, width = 6)

#Immigration plot
tempSummary <- select(QV_data, ImmigrationPref, MaxPoints_QV2NotImmigration, QV2_Class) %>% rename(Class = QV2_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV2 - Immigration (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV2NotImmigration, y = ImmigrationPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Immigration") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV2 - Immigration.png", height = 4, width = 6)

#Public Bonds plot
tempSummary <- select(QV_data, BondsPref, MaxPoints_QV2NotBonds, QV2_Class) %>% rename(Class = QV2_Class) %>% na.omit(tempSummary)
totalSubjects <- nrow(tempSummary)
plotTitle <- paste("QV2 - Public Bonds (N = ", totalSubjects, ")", sep = "")
classSummary <- group_by(tempSummary, Class) %>% summarize(N = n()) %>% mutate(Class = substring(Class, 5))

DotPlot <- ggplot(data = tempSummary, aes(x = MaxPoints_QV2NotBonds, y = BondsPref, colour = Class)) + 
  geom_jitter(width = 3, height = 2) + scale_y_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) +
  geom_abline(intercept = 0, slope = 1) + scale_x_continuous(limits = c(-2,102), breaks = seq(0, 100, by = 10)) + 
  xlab("Max Points Assigned to Other Referenda") + ylab("Points Assigned to Public Bonds") + ggtitle(plotTitle) +
  scale_colour_manual(values = c("(1) Red" = "red", "(2) Yellow" = "yellow3", "(3) Green" = "dark green", "(4) Blue" = "blue")) + 
  theme(axis.text=element_text(size=12, vjust = -.05), legend.text=element_text(size=14), title=element_text(size=14)) +
  annotation_custom(tableGrob(classSummary, rows = NULL), xmin=85, xmax = 102, ymin=62, ymax = 102)
print(DotPlot)
ggsave(DotPlot, file="California Stage 2 - Experiment Plots/BV & QV Relative Preference Scatterplots/QV2 - Public Bonds.png", height = 4, width = 6)


